//
//  BaseViewController.m
//  League
//
//  Created by 博宇 李 on 2018/1/18.
//  Copyright © 2018年 ciyuanjuzhen. All rights reserved.
//

#import "BaseViewController.h"
#import "ErrorView.h"

@interface BaseViewController ()

@property (strong, nonatomic) UIActivityIndicatorView *indicator;

@end

@implementation BaseViewController
-(void)viewWillAppear:(BOOL)animated
   {
       
       
//        if([[RCIMClient sharedRCIMClient] getTotalUnreadCount]> 0){
//            UITabBarItem *item = [self.tabBarController.tabBar.items objectAtIndex:1];
//            item.badgeValue = [NSString stringWithFormat:@"%@",@([[RCIMClient sharedRCIMClient] getTotalUnreadCount])];
//            if (@available(iOS 10.0, *)) {
//                item.badgeColor = kAppColor;
//            } else {
//                // Fallback on earlier versions
//            }
//        }
   }
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    self.navigationItem.backBarButtonItem = item;
    
    [InternationalControl initUserLanguage];//初始化应用语言
    // Do any additional setup after loading the view.
    
    [self.view addSubview:self.indicator];
}

- (void)viewWillDisappear:(BOOL)animated {
    [self.indicator stopAnimating];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)showAlert:(NSString *)desc {
    if ([desc isEqual:[NSNull null]] || desc == nil) {
        return;
    }
    NSBundle *bundle = [InternationalControl bundle];
    [[[UIAlertView alloc] initWithTitle:@"" message:desc delegate:nil cancelButtonTitle:[bundle localizedStringForKey:@"ok" value:nil table:@"hello"] otherButtonTitles:nil, nil] show];
}

- (void)refreshBegin {
    [self.view bringSubviewToFront:self.indicator];
    [self.indicator startAnimating];
}

- (void)refreshStop {
    [self.indicator stopAnimating];
}

- (void)showErrorsWithMessage:(id)obj {
    NSString *message;
    if ([obj isKindOfClass:[NSString class]]) {
        message = obj;
    }
    if ([obj isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dic = obj;
        NSString *lan = [InternationalControl userLanguage];
        if([lan isEqualToString:@"en"]){//判断当前的语言，进行改变
            if (dic[@"msgEn"] == nil || [dic[@"msgEn"] isEqual:[NSNull null]]) {
                if (dic[@"msg"] != nil) {
                    message = dic[@"msg"];
                } else {
                    message = @"Unknown Error";
                }
            } else {
                message = dic[@"msgEn"];
            }
        } else {
            if (dic[@"msg"] == nil || [dic[@"msg"] isEqual:[NSNull null]]) {
                if (dic[@"msgEn"] != nil) {
                    message = dic[@"msgEn"];
                } else {
                    message = @"未知错误";
                }
            } else {
                message = dic[@"msg"];
            }
        }
    }
    ErrorView *error = [[ErrorView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) ErrorMessage:message];
    [[[UIApplication sharedApplication] keyWindow] addSubview:error];
}

//setter and getter
- (UIActivityIndicatorView *)indicator {
    if (_indicator) {
        return _indicator;
    }
    _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _indicator.center = self.view.center;
    return _indicator;
}

- (NSBundle *)languageBundle {
    [InternationalControl initUserLanguage];//初始化应用语言
    NSBundle *bundle = [InternationalControl bundle];
    return bundle;
}

- (NSString *)systemLanguage {
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *language = [def valueForKey:@"userLanguage"];
    return language;
}

- (BaseViewController *)initializeStoryboardViewControllerWithStoryboardID:(NSString *)storyBoard viewcontroller:(NSString *)viewcontroller {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoard bundle:nil];
    BaseViewController *vc = [storyboard instantiateViewControllerWithIdentifier:viewcontroller];
    return vc;
}

@end
